﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class CategoryRepository : Data.Repository.ICategoryRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region ICategoryRepository Members

		/// <summary>
		/// create new category
		/// </summary>
		/// <param name="theNewCategory">the new category entity</param>
		/// <returns>the new category's ID</returns>
		public int CreateNewCategory(Data.Entity.Category theNewCategory)
		{
			if(theNewCategory == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Category_Create(theNewCategory.CategoryName, theNewCategory.Description, theNewCategory.ParentCategoryID,
					theNewCategory.PictureID, theNewCategory.ShowOnHomePage, theNewCategory.Published, theNewCategory.Deleted, theNewCategory.DisplayOrder,
					theNewCategory.CreatedOn, theNewCategory.UpdatedOn, theNewCategory.LanguageID);
			}

			return result;
		}

		/// <summary>
		/// delete category method
		/// </summary>
		/// <param name="id">the deleted category's ID</param>
		/// <returns>1:delete successfull 0:delete failed</returns>
		public int DeleteCategory(int id)
		{
			if(id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Category_Delete(id);
			}

			return result;
		}

		/// <summary>
		/// get all category method
		/// </summary>
		/// <returns>categories</returns>
		public IList<Data.Entity.Category> GetCategoryByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			List<ECommerce_GetProductCategory_By_LanguageResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProductCategory_By_Language(languageID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Category>();

				foreach(var c in lstData)
				{
					var theNewEntity = new Data.Entity.Category()
					{
						CategoryID = c.CategoryID,
						CategoryName = c.CategoryName,
						CreatedOn = c.CreatedOn,
						Deleted = c.Deleted,
						Description = c.Description,
						DisplayOrder = c.DisplayOrder,
						ParentCategoryID = c.ParentCategoryID,
						PictureID = c.PictureID,
						Published = c.Published,
						ShowOnHomePage = c.ShowOnHomePage,
						UpdatedOn = c.UpdatedOn,
						LanguageID = c.LanguageID
					};

					result.Add(theNewEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateCategory(Data.Entity.Category theCategory)
		{
			if(theCategory == null || theCategory.CategoryID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbCategory = (from c in _dbContext.Categories
								  where c.CategoryID == theCategory.CategoryID
								  select c).FirstOrDefault();

				if(dbCategory == null)
				{
					result = 0;
				}

				dbCategory.CategoryName = theCategory.CategoryName;
				dbCategory.Deleted = theCategory.Deleted;
				dbCategory.Description = theCategory.Description;
				dbCategory.DisplayOrder = theCategory.DisplayOrder;
				dbCategory.ParentCategoryID = theCategory.ParentCategoryID;
				dbCategory.PictureID = theCategory.PictureID;
				dbCategory.Published = theCategory.Published;
				dbCategory.ShowOnHomePage = theCategory.ShowOnHomePage;
				dbCategory.UpdatedOn = theCategory.UpdatedOn;
				dbCategory.LanguageID = theCategory.LanguageID;

				_dbContext.SubmitChanges();

				result = 1;
			}

			return result;
		}

		public IList<Data.Entity.Category> GetSubCategories(int parentCategoryID)
		{
			if(parentCategoryID <= 0)
			{
				return null;
			}

			List<ECommerce_GetSubCategoriesResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetSubCategories(parentCategoryID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Category>();

				foreach(var c in lstData)
				{
					var theEntity = new Data.Entity.Category()
					{
						CategoryID = c.CategoryID,
						CategoryName = c.CategoryName,
						CreatedOn = c.CreatedOn,
						Deleted = c.Deleted,
						Description = c.Description,
						DisplayOrder = c.DisplayOrder,
						ParentCategoryID = c.ParentCategoryID,
						PictureID = c.PictureID,
						Published = c.Published,
						ShowOnHomePage = c.ShowOnHomePage,
						UpdatedOn = c.UpdatedOn,
						LanguageID = c.LanguageID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public Data.Entity.Category GetTheCategory(int categoryID)
		{
			if(categoryID <= 0)
			{
				return null;
			}

			System.Collections.Generic.List<ECommerce_GetTheCategoryResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheCategory(categoryID);

				lstData = data.ToList();
			}

			Data.Entity.Category result;

			if(lstData != null && lstData.Count > 0)
			{
				var c = lstData[0];

				result = new Data.Entity.Category()
				{
					CategoryID = c.CategoryID,
					CategoryName = c.CategoryName,
					CreatedOn = c.CreatedOn,
					Deleted = c.Deleted,
					Description = c.Description,
					DisplayOrder = c.DisplayOrder,
					ParentCategoryID = c.ParentCategoryID,
					PictureID = c.PictureID,
					Published = c.Published,
					ShowOnHomePage = c.ShowOnHomePage,
					UpdatedOn = c.UpdatedOn,
					LanguageID = c.LanguageID
				};

				return result;
			}

			return null;
		}

		//public IList<Data.Entity.Category> GetAllCategoryByLanguage(int languageID)
		//{
		//    if(languageID <= 0)
		//    {
		//        return null;
		//    }

		//    List<Data.Category>

		//    if(_dbContext == null)
		//    {
		//        _dbContext = new LinqToECommerceDBDataContext();
		//    }

		//    return (from c in _dbContext.Categories
		//            where !c.Deleted
		//            select new Entity.Category
		//            {
		//                CategoryID = c.CategoryID,
		//                CategoryName = c.CategoryName,
		//                CreatedOn = c.CreatedOn,
		//                Deleted = c.Deleted,
		//                Description = c.Description,
		//                DisplayOrder = c.DisplayOrder,
		//                ParentCategoryID = c.ParentCategoryID,
		//                PictureID = c.PictureID,
		//                Published = c.Published,
		//                ShowOnHomePage = c.ShowOnHomePage,
		//                UpdatedOn = c.UpdatedOn,
		//                LanguageID = c.LanguageID
		//            }).AsQueryable();
		//}

		#endregion
	}
}
